﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using Reborn_Server.App1AppHandler;

namespace Reborn_WorldServer.PendingTells
{
    public class TellManagerWS
    {
        private List<TellRequestWS> tellRequests = new List<TellRequestWS>();

        public TellManagerWS() { }

        //Called by TellRequestWS if it times out, removes from pending queue and processes the request
        public void tellRequestTimeout(TellRequestWS request)
        {
            lock (tellRequests)
            {
                tellRequests.Remove(request);
                request.processResult(false);
            }
        }

        //this is called when a responce from the primary server is recieved
        //removes the tell request from queue and processes the result
        public void processResult(bool messageSuccessfull, string tellGUID, string fromName, string toName)
        {
            lock (tellRequests)
            {
                foreach (TellRequestWS request in tellRequests)
                {
                    if (request.tellGUID == tellGUID && request.fromCharacter.Name.ToLower().Trim() == fromName.ToLower().Trim() && 
                        request.toCharacterName.ToLower().Trim() == toName.ToLower().Trim())
                    {
                        tellRequests.Remove(request);
                        request.processResult(messageSuccessfull);
                        break;
                    }
                }
            }
        }

        public bool sendRequest(TellRequestWS newTellRequest)
        {
            lock (tellRequests)
            {
                tellRequests.Add(newTellRequest);
            }

            StringWriter stringWriter = new StringWriter();
            XmlTextWriter xmldoc = new XmlTextWriter(stringWriter);

            xmldoc.WriteStartElement("tellrequestWS");

            xmldoc.WriteAttributeString("tellguid", newTellRequest.tellGUID);
            xmldoc.WriteAttributeString("fromname", newTellRequest.fromCharacter.Name);
            xmldoc.WriteAttributeString("toname", newTellRequest.toCharacterName);

            xmldoc.WriteStartElement("txt");
            xmldoc.WriteRaw(Message.escapeXmlText(newTellRequest.message));
            xmldoc.WriteEndElement();

            xmldoc.WriteRaw(newTellRequest.itemLinksXml);

            xmldoc.WriteEndElement();

            xmldoc.Flush();
            xmldoc.Close();

            stringWriter.Flush();

            string xmlMessage = stringWriter.ToString();

            if (Managers.SendToPrimary(xmlMessage) == false)
            {
                lock (tellRequests)
                {
                    tellRequests.Remove(newTellRequest);
                    newTellRequest.killTimeout();
                    return false;
                }
            }

            return true;
        }
    }
}
